package com.qf.shopping.controll.carControll;

import com.qf.shopping.pojo.car.CarMake;
import com.qf.shopping.service.carService.ICarMakeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Humy
 * @date 2019/4/29 15:57
 */

@Controller
@RequestMapping("/carMake")
public class CarMakeControll {

    @Autowired
    private ICarMakeService carMakeService;

    /**
     * 加载所有的汽车品牌
     * @param model
     * @return
     */
    @GetMapping("/getAllCarMakes")
    public String getAllCarMakes(Model model){
        List<CarMake> carMakes = carMakeService.queryAll();
        model.addAttribute("carMakes",carMakes);

        return "car/carMake/list";
    }

    /**
     * 根据条件查询品牌
     * @return
     */
    @RequestMapping("/getCarMakes")
    public String getCarMakes( CarMake carMake){

        List<CarMake> carMakes = carMakeService.query(carMake);

        return "car/carMake/list";
    }

    /**
     * 根据id查询品牌
     */
    @RequestMapping("/getCarMake")
    @ResponseBody
    public CarMake getCarmake(Long id){
        CarMake carMake = carMakeService.queryById(id);
        return carMake;
    }

    /**
     * 新增品牌
     */
    @RequestMapping("/addCarMake")
    public String addCarMake(CarMake carMake){

        carMakeService.add(carMake);

        return "redirect:/carMake/getAllCarMakes";
    }

    /**
     * 加载要编辑的品牌
     */
    @GetMapping("/updateCarMake/{id}")
    public CarMake updateCarMake(Long id){

        CarMake carMake = carMakeService.queryById(id);

        return carMake;
    }

    /**
     * 提交编辑
     */
    @PostMapping("/updateCarMake")
    public String updateCarMake(CarMake carMake){
        carMakeService.update(carMake);
        return "redirect:/carMake/getAllCarMakes";
    }

    /**
     * 删除品牌
     */
    @RequestMapping("deleteCarMake/{id}")
    public String deleteCarMake(@PathVariable("id")Long...ids){

        carMakeService.delete(ids);

        return "redirect:/carMake/getAllCarMakes";
    }


}
